using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetPOSTaggedText : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        private HighlightManager oHighlighting;

        public ctrlGetPOSTaggedText(RichTextBox oTextBox)
        {
            InitializeComponent();
            oHighlighting = new HighlightManager();
            defineHighlightColors();
            oHighlighting.CreateLegend(legendPanel);
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }
        
        public void processText()
        {
            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                string strPOSTaggedText = SalienceDemo.oSalienceSession.GetPOSTaggedText();
                strPOSTaggedText = strPOSTaggedText.Replace("LXA_``", "LXA_''");
                this.wbPOSMarkup.DocumentText = oHighlighting.FormatResults(strPOSTaggedText);

            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void defineHighlightColors()
        {
            //entities of the following types will be colored and have a key in the entities legend section.
            oHighlighting.AddHighlight("Noun", "#FFFF00"); //Yellow
            oHighlighting.AddHighlight("Verb", "#00FFFF"); //Aquamarine
            oHighlighting.AddHighlight("Modifier", "#00FF00"); //Green
            oHighlighting.AddHighlight("Determiner", "#EE99BB"); //Pink
            oHighlighting.AddHighlight("Symbol", "#BB44CC"); //Light Blue
            oHighlighting.AddHighlight("Misc", "#CCCCCC"); //Light Gray
            oHighlighting.AddHighlight("Twitter", "#FFA801"); //Orange            

            // Noun class
            oHighlighting.AddSynonym("NNP", "Noun");
            oHighlighting.AddSynonym("NNPS", "Noun");
            oHighlighting.AddSynonym("NN", "Noun");
            oHighlighting.AddSynonym("NNS", "Noun");
            oHighlighting.AddSynonym("PRP$", "Noun");
            oHighlighting.AddSynonym("PRP", "Noun");
            oHighlighting.AddSynonym("WP", "Noun");
            oHighlighting.AddSynonym("WP$", "Noun");

            // Verb class
            oHighlighting.AddSynonym("MD", "Verb");
            oHighlighting.AddSynonym("VB", "Verb");
            oHighlighting.AddSynonym("VBC", "Verb");
            oHighlighting.AddSynonym("VBD", "Verb");
            oHighlighting.AddSynonym("VBF", "Verb");
            oHighlighting.AddSynonym("VBG", "Verb");
            oHighlighting.AddSynonym("VBN", "Verb");
            oHighlighting.AddSynonym("VBP", "Verb");
            oHighlighting.AddSynonym("VBZ", "Verb");

            // Adjective/adverb class
            oHighlighting.AddSynonym("JJ", "Modifier");
            oHighlighting.AddSynonym("JJR", "Modifier");
            oHighlighting.AddSynonym("JJS", "Modifier");
            oHighlighting.AddSynonym("RB", "Modifier");
            oHighlighting.AddSynonym("RBR", "Modifier");
            oHighlighting.AddSynonym("RBS", "Modifier");
            oHighlighting.AddSynonym("WRB", "Modifier");

            // Determiner class
            oHighlighting.AddSynonym("DT", "Determiner");
            oHighlighting.AddSynonym("PDT", "Determiner");
            oHighlighting.AddSynonym("WDT", "Determiner");

            // Misc class
            oHighlighting.AddSynonym("CD", "Misc");
            oHighlighting.AddSynonym("DAT", "Misc");
            oHighlighting.AddSynonym("CC", "Misc");
            oHighlighting.AddSynonym("EX", "Misc");
            oHighlighting.AddSynonym("FW", "Misc");
            oHighlighting.AddSynonym("IN", "Misc");
            oHighlighting.AddSynonym("RP", "Misc");
            oHighlighting.AddSynonym("TO", "Misc");
            oHighlighting.AddSynonym("UH", "Misc");
            oHighlighting.AddSynonym("POS", "Misc");

            // Twitter class
            oHighlighting.AddSynonym("URL", "Twitter");
            oHighlighting.AddSynonym("USER", "Twitter");
            oHighlighting.AddSynonym("EMAIL", "Twitter");
            oHighlighting.AddSynonym("NNPOS", "Twitter");
            oHighlighting.AddSynonym("UR", "Twitter");
            //oHighlighting.AddSynonym(":)", "Twitter");
            oHighlighting.AddSynonym("#TAG", "Twitter");
            oHighlighting.AddSynonym("!!!", "Twitter");
            //oHighlighting.AddSynonym("...", "Twitter");

            // Symbol class
            oHighlighting.AddSynonym("SYM", "Symbol");
            oHighlighting.AddSynonym("LRB", "Symbol");
            oHighlighting.AddSynonym("RRB", "Symbol");
            oHighlighting.AddSynonym(",", "Symbol");
            oHighlighting.AddSynonym("-", "Symbol");
            oHighlighting.AddSynonym(":", "Symbol");
            oHighlighting.AddSynonym(";", "Symbol");
            oHighlighting.AddSynonym(".", "Symbol");
            oHighlighting.AddSynonym("''", "Symbol");
            //oHighlighting.AddSynonym("``", "Symbol");
            //oHighlighting.AddSynonym("\"", "Symbol");
            oHighlighting.AddSynonym("$", "Symbol");

        }

        private void getPOSTaggedText_VisibleChanged(object sender, EventArgs e)
        {
            wbPOSMarkup.DocumentText = "";
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.NOTHING);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            if (this.wbPOSMarkup.Document.Body != null)
            {
                Graphics g = this.CreateGraphics();
                float pixels = Properties.Settings.Default.fFontSize * g.DpiX / 72;
                g.Dispose();
                this.wbPOSMarkup.Document.Body.Style = "font-size:" + pixels + "px;";
            }
        }

        public void EnableProcessButton()
        {
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }

        #endregion
    }
}
